package cn.stu.projectsummit.school.service.impl;

import cn.stu.projectsummit.blog.mapper.BlogMapper;
import cn.stu.projectsummit.comment.mapper.CommentMapper;
import cn.stu.projectsummit.school.mapper.SchoolMapper;
import cn.stu.projectsummit.school.pojo.dto.SchoolQuery;
import cn.stu.projectsummit.school.pojo.dto.SchoolSaveParam;
import cn.stu.projectsummit.school.pojo.entity.School;
import cn.stu.projectsummit.school.pojo.vo.SchoolVO;
import cn.stu.projectsummit.school.pojo.dto.SchoolSaveParam;
import cn.stu.projectsummit.school.pojo.dto.SchoolSearch;
import cn.stu.projectsummit.school.pojo.entity.School;
import cn.stu.projectsummit.school.pojo.vo.SchoolCount;
import cn.stu.projectsummit.school.service.SchoolService;
import cn.stu.projectsummit.user.mapper.UserMapper;
import cn.stu.projectsummit.user.pojo.dto.UserQuery;
import cn.stu.projectsummit.user.pojo.entity.User;
import cn.stu.projectsummit.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SchoolServiceImpl implements SchoolService {
    @Autowired
    SchoolMapper schoolMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    BlogMapper blogMapper;
    @Autowired
    CommentMapper commentMapper;
    @Override
    public List<SchoolVO> selectSchool(SchoolQuery schoolQuery) {
        log.debug("查询学校业务,schoolSaveParam={}",schoolQuery);
        List<SchoolVO> list = schoolMapper.selectSchool(schoolQuery);
        for (int i =0 ;i<list.size();i++){
            SchoolVO schoolVO = list.get(i);
            getUserName(schoolVO);
        }

        return list;
    }
    private void getUserName(SchoolVO schoolVO){
        UserQuery userQuery = new UserQuery();
        userQuery.setSchoolId(schoolVO.getId());
        List<UserVO> userVOS = userMapper.selectUser(userQuery);
        for (int i =0 ;i<userVOS.size();i++){
            schoolVO.setUsername(userVOS.get(i).getUsername());
        }
    }


    @Override
    public void deleteSchool(Long id) {
        schoolMapper.deleteSchool(id);
    }

    @Override
    public void saveSchool(SchoolSaveParam schoolSaveParam) {
        log.debug("保存学校业务,schoolSaveParam={}",schoolSaveParam);
        if (schoolSaveParam.getId() == null){
            schoolMapper.insert(schoolSaveParam);
        }else {
            schoolMapper.updateSchool(schoolSaveParam);
        }
    }

    @Override
    public List<SchoolCount> count(SchoolSearch schoolSearch) {
        log.debug("登录业务,参数:{}");
        log.debug("&&&&&&&&&&&&"+schoolSearch.getId()+schoolSearch.getSchoolName());
        List<SchoolCount> list = new ArrayList<>();
        UserQuery userQuery = new UserQuery();
        List<SchoolVO> schoolList = schoolMapper.selectSchool(schoolSearch);
        log.debug("@@@@@@@@@@@@@"+schoolList.size());
        for (int i = 0; i < schoolList.size(); i++) {
            SchoolVO school = schoolList.get(i);
            userQuery.setSchoolId(school.getId());
            log.debug("!!!!!!!!!!!!!!!!!"+school.getId());
            List<UserVO> userList=userMapper.selectUser(userQuery);
            Integer commentSum = 0;
            Integer blogSum = 0;
            List<String> nameList = new ArrayList<>();
            for (int j = 0; j < userList.size(); j++) {
                UserVO userVO = userList.get(j);
                nameList.add(userVO.getUsername());
                Long userId = userVO.getId();
                Integer commentCount = commentMapper.selectCommentCount(userId);
                Integer blogCount = blogMapper.selectBlogCount(userId);
                commentSum += commentCount;
                blogSum += blogCount;
            }
            log.debug("*****************"+userList.size());
            SchoolCount schoolCount = new SchoolCount();
            schoolCount.setSchoolName(school.getSchoolName());
            String concatenatedNames = String.join(", ", nameList);
            schoolCount.setUserName(concatenatedNames);
            schoolCount.setBlogCount(blogSum);
            schoolCount.setCommentCount(commentSum);
            list.add(schoolCount);

        }
        return list;
    }
}
